package poo.file;

import java.io.*;
import java.util.*;

public class AggiornamentoSelettivo {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		System.out.print("Nome file: "); //C:\Users\Pa\Desktop\f3.dat
		String nome = sc.nextLine();
		System.out.print("intero da aggiungere");
		int x = sc.nextInt();
		inserisci(nome, x);
	}// main

	static void inserisci(String nome, int x) throws IOException {
		RandomAccessFile raf = new RandomAccessFile(nome, "r");
		DataOutputStream tmp = new DataOutputStream(new FileOutputStream("tmp"));
		long pos = 0;
		int y = 0;
		boolean flag = false;
		while (pos < raf.length() && !flag) {
			y = raf.readInt();
			if (y > x)
				flag = true;
			else {
				tmp.writeInt(y);
				pos = raf.getFilePointer();
			}
		}// while
		tmp.writeInt(x); // scrivi sicuramente x
		if (flag) {
			for (;;) {
				tmp.writeInt(y);
				pos = raf.getFilePointer();
				if (pos == raf.length())
					break;
				y = raf.readInt();
			}// for
		}// if(flag)
		tmp.close();
	}// inserisci

}
